<?php

namespace addons\shopro\model;

use think\Model;
use app\common\model\MoneyLog;

/**
 * 进入冻结积分账号明细
 */
class UserTotalScroeLog extends Model
{

    // 表名,不含前缀
    protected $name = 'user_total_score_log';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;


    public static function write($user_id,$scroe, $points, $typeid, $type, $remark)
    {
        $self = self::create([
            "userid" => $user_id,
            'scroe'  =>$scroe,
            "typeid" => $typeid,
            "type" => $type,
            "points" => $points,
            "remark" => $remark,
        ]);
        return $self;
    }



    public static function getList($params)
    {
        $user_id = User::info()->id;
        $user = User::get($user_id);

        // 分页列表
        $walletLogs = self::buildSearch($params)->order('id', 'DESC')->paginate(10);
        // 收入
        $income = self::buildSearch($params)->where('wallet', '>=', 0)->sum('wallet');
        // 支出
        $expend = self::buildSearch($params)->where('wallet', '<', 0)->sum('wallet');

        foreach ($walletLogs as $w) {
            $w->avatar = $user->avatar;
            switch ($w['type']) {
                case 'wallet_pay':
                    $item = OrderItem::where('order_id', $w->item_id)->order('id', 'asc')->find();
                    $w->avatar = $item['goods_image'] ?? '';
                    $w->title = $item['goods_title'] ?? '';
                    break;
                case 'wallet_refund':
                    $ext = json_decode($w->ext, true);
                    $item_id = $ext['item_id'] ?? 0;
                    if ($item_id) {
                        $item = OrderItem::get($item_id);
                    } else {
                        $item = OrderItem::where('order_id', $w->item_id)->order('id', 'asc')->find();
                    }

                    $w->avatar = $item['goods_image'] ?? '';
                    $w->title = $item['goods_title'] ?? '';
                    break;
                case 'cash':
                case 'cash_error':
                    $w->avatar = $user->avatar;
                    break;
            }
        }
        return [
            'wallet_logs' => $walletLogs,
            'income' => $income,
            'expend' => $expend,
        ];
    }


    private static function buildSearch($params)
    {
        $user = User::info();
        $status = $params['status'] ?? 'all';
        $wallet_type = $params['wallet_type'] ?? 'money';
        $date = isset($params['date']) ? explode('-', $params['date']) : [];
        $start = isset($date[0]) && $date[0] ? strtotime($date[0]) : strtotime(date('Y-m') . '-01');
        $end = isset($date[1]) && $date[1] ? (strtotime($date[1]) + 86399) : strtotime(date('Y-m-d')) + 86399;

        $walletLogs = self::{$wallet_type}();

        if ($status != 'all') {
            $walletLogs = $walletLogs->{$status}();
        }

        $walletLogs = $walletLogs->where(['user_id' => $user->id])
            ->whereBetween('createtime', [$start, $end]);

        return $walletLogs;
    }



    public static function getTypeName($type)
    {
        return isset(self::$typeAll[$type]) ? self::$typeAll[$type]['name'] : '';
    }


    public function getTypeNameAttr($value, $data)
    {
        return self::getTypeName($data['type']);
    }


    public function getWalletTypeNameAttr($value, $data)
    {
        return self::$walletTypeAll[$data['wallet_type']] ?? '';
    }


    public function getWalletAttr($value, $data)
    {
        return $data['wallet_type'] == 'score' ? intval($value) : $value;
    }
}
